class Solution(object):
    def isAdditiveNumber(self, num):
        """
        :type num: str
        :rtype: bool
        """
        if len(num) < 3:
            return False
        n = len(num)
        if num[0] == '0':
            for j in range(1, n - 1):
                s1 = 0
                s2 = int(num[1: j + 1])
                s = str(s1 + s2)
                index = j + 1
                while index < n:
                    if index + len(s) <= n and s == num[index: index + len(s)]:
                        s1 = s2
                        s2 = int(s)
                        index += len(s)
                        s = str(s1 + s2)
                    else:
                        break
                if index == n:
                    return True
            return False
        else:
            for i in range(1, n - 1):
                if num[i] == '0':
                    s1 = int(num[:i])
                    s2 = 0
                    s = str(s1 + s2)
                    index = i + 1
                    while index < n:
                        if index + len(s) <= n and s == num[index: index + len(s)]:
                            s1 = s2
                            s2 = int(s)
                            index += len(s)
                            s = str(s1 + s2)
                        else:
                            break
                    if index == n:
                        return True
                else:
                    for j in range(i, n - 1):
                        s1 = int(num[:i])
                        s2 = int(num[i: j + 1])
                        s = str(s1 + s2)
                        index = j + 1
                        while index < n:
                            if index + len(s) <= n and s == num[index: index + len(s)]:
                                s1 = s2
                                s2 = int(s)
                                index += len(s)
                                s = str(s1 + s2)
                            else:
                                break
                        if index == n:
                            return True
        return False


data = Solution()
# num = "1234"
# print(data.isAdditiveNumber(num))
num = "111122335588143"
print(data.isAdditiveNumber(num))
